package wangjg.xuexiban.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

/**
 * @author wangjg
 * 
 */
class AppConfig {
	public static final AppConfig instance = new AppConfig();

	private Properties settings = new Properties();
	private boolean watching=false;
	private File file ;
	private long lastModify ;

	private AppConfig() {
		loadConfig();
	}
	
	private void loadConfig() {
		String filepath = "/conf.properties";
		this.loadConfig(filepath);
	}
	
	public static void main(String args[]){
		AppConfig.instance.watch();
	}
	
	public void watch(){
		if(watching)return;
		watching=true;
		Thread t = new Thread(){
			public void run(){
				while(true){
					long l = file.lastModified();
					if(l!=lastModify){
						loadConfig();
						lastModify = file.lastModified();
					}
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		};
		t.start();
	}
	
	public void loadConfig(String filepath) {
		InputStream is = null;
		try {
			URL url = getClass().getResource(filepath);
			this.file = new File(url.toURI());
			this.lastModify = file.lastModified();
			FileInputStream fis = new FileInputStream(file);
			is = fis;
			
			is = getClass().getResourceAsStream(filepath);
			int start = filepath.lastIndexOf(".");
			if (filepath.substring(start).toLowerCase().equals(".xml")) {
				settings.loadFromXML(is);
			} else {
				settings.load(is);
			}
		} catch (Exception e) {
			throw new RuntimeException("load configuration error, filepath:"+filepath,e);
		}finally{
			try {
				if(is!=null)is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public Properties getSettings() {
		return settings;
	}

	public String getString(String key) {
		String value = settings.getProperty(key);
		if(value==null){
			if(key!=null && key.startsWith("{") && key.endsWith("}")){
				value = System.getProperty(key.substring(1, key.length()-1));
			}
		}
		return value;
	}

	public Boolean getBoolean(String key) {
		String value = this.getString(key);
		if(value==null)return null;
		return Boolean.valueOf(value);
	}

	public Boolean getBoolean(String key,boolean defaultValue) {
		String value = this.getString(key);
		if(value==null)return defaultValue;
		return Boolean.valueOf(value);
	}
	
	public Short getShort(String key, short defaultValue) {
		Short v = this.getShort(key);
		return v!=null ? v : defaultValue;
	}
	
	public Short getShort(String key) {
		String value = this.getString(key);
		if(value==null)return null;
		return Short.valueOf(value);
	}
	
	public Integer getInteger(String key, int defaultValue) {
		Integer v = this.getInteger(key);
		return v!=null ? v : defaultValue;
	}
	
	public Integer getInteger(String key) {
		String value = this.getString(key);
		if(value==null)return null;
		return Integer.valueOf(value);
	}
	
	public Long getLong(String key, long defaultValue) {
		Long v = this.getLong(key);
		return v!=null ? v : defaultValue;
	}
	
	public Long getLong(String key) {
		String value = this.getString(key);
		if(value==null)return null;
		return Long.valueOf(value);
	}

}